﻿Imports System.Globalization
Imports System.IO
Partial Public Class Marker_Report
    Inherits System.Web.UI.Page

    Dim clsGet As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "CN\DN REPORT "
            txtDate.Text = Date.Now.AddMonths(-1).ToString("dd MMM yyyy", New CultureInfo("en-US"))
            txtDateTo.Text = Date.Now.ToString("dd MMM yyyy", New CultureInfo("en-US"))
        End If
    End Sub
    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Sub gridMarker_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridMarker.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim Grid As GridView = CType(sender, GridView)
            e.Row.Cells.Clear()
            Dim GridRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow1 As GridViewRow = New GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow2 As GridViewRow = New GridViewRow(2, 0, DataControlRowType.Header, DataControlRowState.Insert)

            Dim table As TableCell = New TableCell()
            table.Text = "วัน เดือน ปี"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "ปริมาณสาร Marker(ก.ก)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 9
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "ปริมาณน้ำมันที่เติมสาร Marker"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow2.Cells.Add(table)


            table = New TableCell()
            table.Text = "ภษ. 01-28/2 ทะเบียนรับ เลขที่"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "ภษ. 01-28 ทะเบียนรับ เลขที่"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "ภษ. 01-36 ทะเบียนรับ เลขที่"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "หมายเหตุ"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "หมายเลขถัง ที่รับจาก Shore"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(0, GridRow2)

            table = New TableCell()
            table.Text = "ยอดยกมา"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "ปริมาณรับ"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "ปริมาณจ่าย (ตามสูตร)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "ปริมาณจ่าย (เพิ่ม)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "ปริมาณจ่าย (จริง)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)


            table = New TableCell()
            table.Text = "การโอน"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "เสียหาย"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "คงเหลือ"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "ส่งออก (ลิตร)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)


            table = New TableCell()
            table.Text = "เติมเรือ (ลิตร)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(1, GridRow1)

            table = New TableCell()
            table.Text = "รับโอน"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "จ่ายโอน"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(2, GridRow)
        End If
    End Sub

    Private Function CreateDT() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("DateMarker", GetType(String))
        dt.Columns.Add("Open", GetType(String))
        dt.Columns.Add("Recieve", GetType(String))
        dt.Columns.Add("Tran1", GetType(String))
        dt.Columns.Add("Tran2", GetType(String))
        dt.Columns.Add("Tran3", GetType(String))
        dt.Columns.Add("R_Tran", GetType(String))
        dt.Columns.Add("T_Tran", GetType(String))
        dt.Columns.Add("Loss", GetType(String))
        dt.Columns.Add("Total", GetType(String))
        dt.Columns.Add("External", GetType(String))
        dt.Columns.Add("Plus", GetType(String))
        dt.Columns.Add("P_1", GetType(String))
        dt.Columns.Add("P_2", GetType(String))
        dt.Columns.Add("P_3", GetType(String))
        dt.Columns.Add("Remark", GetType(String))
        dt.Columns.Add("Shore", GetType(String))
        dt.Columns.Add("Sum1", GetType(String))
        dt.Columns.Add("Sum2", GetType(String))
        dt.Columns.Add("Sum3", GetType(String))
        dt.Columns.Add("Sum4", GetType(String))
        dt.Columns.Add("Sum5", GetType(String))
        dt.Columns.Add("Sum6", GetType(String))
        dt.Columns.Add("_D", GetType(String))
        dt.Columns.Add("Stock_Code", GetType(String))
        dt.AcceptChanges()

        Return dt
    End Function

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        If (InsertValue()) Then
            Dim service As New _ReportingService.Report

            Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
            Dim Orientation As Boolean = True
            Dim ReportName As String = String.Empty
            Dim reportDataSource As String = String.Empty
            Dim paraName As String = String.Empty
            Dim paraValue As String = String.Empty
            Dim reportType As String = String.Empty
            Dim urlDisplay As String = String.Empty
            Dim dsReport As New DataSet
            Dim dt As DataTable = CreateDT()
            Dim Sum1 As Double = 0
            Dim Sum2 As Double = 0
            Dim Sum3 As Double = 0
            Dim Sum4 As Double = 0
            Dim Sum5 As Double = 0
            Dim Sum6 As Double = 0
            For i As Integer = 0 To gridMarker.Rows.Count - 1
                Dim txtRecieve As TextBox = CType(gridMarker.Rows(i).FindControl("txtRecieve"), TextBox)
                Dim txtLoss As TextBox = CType(gridMarker.Rows(i).FindControl("txtLoss"), TextBox)
                Dim txtPlus As TextBox = CType(gridMarker.Rows(i).FindControl("txtPlus"), TextBox)
                Dim txt282 As TextBox = CType(gridMarker.Rows(i).FindControl("txt282"), TextBox)
                Dim txt28 As TextBox = CType(gridMarker.Rows(i).FindControl("txt28"), TextBox)
                Dim txt36 As TextBox = CType(gridMarker.Rows(i).FindControl("txt36"), TextBox)
                Dim txtShore As TextBox = CType(gridMarker.Rows(i).FindControl("txtShore"), TextBox)
                Dim dr As DataRow = dt.NewRow

                dr.Item("DateMarker") = gridMarker.Rows(i).Cells(0).Text
                dr.Item("Open") = gridMarker.Rows(i).Cells(1).Text
                If (txtRecieve.Text = "") Then
                    dr.Item("Recieve") = "0"
                Else
                    dr.Item("Recieve") = txtRecieve.Text
                End If

                dr.Item("Tran1") = gridMarker.Rows(i).Cells(3).Text
                dr.Item("Tran2") = gridMarker.Rows(i).Cells(4).Text
                dr.Item("Tran3") = gridMarker.Rows(i).Cells(5).Text
                dr.Item("R_Tran") = gridMarker.Rows(i).Cells(6).Text
                dr.Item("T_Tran") = gridMarker.Rows(i).Cells(7).Text
                If (txtLoss.Text = "") Then
                    dr.Item("Loss") = "0"
                Else
                    dr.Item("Loss") = txtLoss.Text
                End If

                dr.Item("Total") = gridMarker.Rows(i).Cells(9).Text
                dr.Item("External") = gridMarker.Rows(i).Cells(10).Text
                If (txtPlus.Text = "") Then
                    dr.Item("Plus") = "0"
                Else
                    dr.Item("Plus") = txtPlus.Text
                End If

                dr.Item("P_1") = txt282.Text
                dr.Item("P_2") = txt28.Text
                dr.Item("P_3") = txt36.Text
                If (gridMarker.Rows(i).Cells(15).Text = "&nbsp;") Then
                    dr.Item("Remark") = ""
                Else
                    dr.Item("Remark") = gridMarker.Rows(i).Cells(15).Text
                End If

                dr.Item("Shore") = txtShore.Text
                Sum1 += CDbl(gridMarker.Rows(i).Cells(3).Text)
                Sum2 += CDbl(gridMarker.Rows(i).Cells(4).Text)
                Sum3 += CDbl(gridMarker.Rows(i).Cells(5).Text)
                Sum4 += CDbl(gridMarker.Rows(i).Cells(6).Text)
                Try
                    Sum5 += CDbl(txtLoss.Text)
                Catch ex As Exception

                End Try

                Sum6 += CDbl(gridMarker.Rows(i).Cells(10).Text)
                dt.Rows.Add(dr)
                dt.AcceptChanges()
            Next
            For Each dr As DataRow In dt.Rows
                dr.Item("Sum1") = Sum1
                dr.Item("Sum2") = Sum2
                dr.Item("Sum3") = Sum3
                dr.Item("Sum4") = Sum4
                dr.Item("Sum5") = Sum5
                dr.Item("Sum6") = Sum6
                dt.AcceptChanges()
            Next

            dsReport.Tables.Add(dt)
            dsReport.AcceptChanges()
            ReportName = "Marker_Report.rdlc"
            ' dsReport = New DataSet
            reportDataSource = "Marker_Value"
            paraName = "DateTimeNow"
            paraValue = DateTime.Now.ToString()
            Orientation = False
            reportType = radioType.SelectedValue

            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

            Dim oldFile As String = String.Empty
            oldFile = Server.MapPath(urlDisplay)
            Dim fileInfo As FileInfo = New FileInfo(oldFile)
            Dim reportPath As String = "../WriteReport/"

            Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
            newFileName = "Marker_Report" & newFileName

            If fileInfo.Exists Then
                fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
                urlDisplay = reportPath & newFileName & fileInfo.Extension
            End If

            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        End If

    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        Dim dt As DataTable = CreateDT()
        Dim L As Integer = (CDate(txtDateTo.Text) - CDate(txtDate.Text)).Days
        Dim Marker As DataTable = clsGet.Get_StockMaster("", "", "", "'M'")
        If (L < 0) Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ระบุข้อมูลเวลาไม่ถูกต้อง !!');", True)
            Exit Sub
        End If
        For i As Integer = 0 To L
            Dim dr As DataRow = dt.NewRow
            Dim _D As String = CDate(txtDate.Text).AddYears(543).AddDays(i).ToString("yyyyMMdd", New CultureInfo("en-US"))
            dr.Item("DateMarker") = CDate(txtDate.Text).AddYears(543).AddDays(i).ToString("dd/MM/yy", New CultureInfo("en-US"))
            dr.Item("Sum1") = ""
            dr.Item("Sum2") = ""
            dr.Item("Sum3") = ""
            dr.Item("Sum4") = ""
            dr.Item("Sum5") = ""
            dr.Item("Sum6") = ""
            'OPEN
            Dim dtAction As DataTable = clsGet.Get_StockActionToCurrent(Marker.Rows(0).Item("Stock_Code"), _D, "99999999")
            Dim Open As Double = CDbl(Marker.Rows(0).Item("Amount"))
            If (dtAction.Rows.Count > 0) Then
                For Each da As DataRow In dtAction.Rows
                    If (da.Item("Type_Event") & "" = "A") Then
                        Open -= CDbl(da.Item("Amount"))
                    Else
                        Open += CDbl(da.Item("Amount"))
                    End If
                    If (da.Item("Type_Event") & "" = "C") Then
                        Dim dtEx As DataTable = clsGet.Get_Load_Unload_Addition(da.Item("Booking_no"))
                        If (dtEx.Rows.Count > 0) Then
                            If (dtEx.Rows(0).Item("Excise") & "" <> "0") Then
                                Dim Relation As DataTable = clsGet.Get_Stock_Relation(da.Item("Stock_Code"))
                                If (Relation.Rows.Count > 0) Then
                                    Try
                                        Open += CDbl(dtEx.Rows(0).Item("Excise")) * CDbl(Relation.Rows(0).Item("Marker_Point"))
                                    Catch ex As Exception

                                    End Try

                                End If
                            End If
                        End If
                    End If
                Next
            End If
            dr.Item("Open") = Open
            dr.Item("_D") = _D
            dr.Item("Stock_Code") = Marker.Rows(0).Item("Stock_Code")
            dtAction = clsGet.Get_Marker_Report_Point(Marker.Rows(0).Item("Stock_Code"), _D)
            If (dtAction.Rows.Count > 0) Then
                dr.Item("Recieve") = dtAction.Rows(0).Item("Recieve")
                dr.Item("Loss") = dtAction.Rows(0).Item("Loss")
                dr.Item("Plus") = dtAction.Rows(0).Item("Plus")
                dr.Item("P_1") = dtAction.Rows(0).Item("_282")
                dr.Item("P_2") = dtAction.Rows(0).Item("_28")
                dr.Item("P_3") = dtAction.Rows(0).Item("_36")
                dr.Item("Shore") = dtAction.Rows(0).Item("Shore")
            Else
                dr.Item("Recieve") = "0"
                dr.Item("Loss") = "0"
                dr.Item("Plus") = "0"
                dr.Item("P_1") = ""
                dr.Item("P_2") = ""
                dr.Item("P_3") = ""
                dr.Item("Shore") = ""
            End If

            dtAction = clsGet.Get_StockActionToCurrent(Marker.Rows(0).Item("Stock_Code"), _D, _D)
            Dim Recieve As Double = 0
            Dim Transfer As Double = 0
            Dim Cut As Double = 0
            Dim Excise As Double = 0
            Dim BL As Double = 0
            Dim remark As String = ""
            If (dtAction.Rows.Count > 0) Then
                For Each da As DataRow In dtAction.Rows
                    If (da.Item("Type_Event") & "" = "A") Then
                        Recieve += CDbl(da.Item("Amount"))
                    ElseIf (da.Item("Type_Event") & "" = "R") Then
                        Transfer += CDbl(da.Item("Amount"))
                    ElseIf (da.Item("Type_Event") & "" = "C") Then
                        Cut += CDbl(da.Item("Amount"))
                        Dim dtEx As DataTable = clsGet.Get_Load_Unload_Addition(da.Item("Booking_no"))
                        If (dtEx.Rows.Count > 0) Then
                            If (dtEx.Rows(0).Item("Excise") & "" <> "0") Then
                                Dim Relation As DataTable = clsGet.Get_Stock_Relation(da.Item("Stock_Code"))
                                If (Relation.Rows.Count > 0) Then
                                    Try
                                        Excise += CDbl(dtEx.Rows(0).Item("Excise")) * CDbl(Relation.Rows(0).Item("Marker_Point"))
                                    Catch ex As Exception

                                    End Try

                                End If
                            End If
                        End If
                        dtEx = clsGet.Get_Load_Unload_Product(da.Item("Booking_no"), "")
                        For Each dm As DataRow In dtEx.Rows
                            Try
                                BL += CDbl(dm.Item("LU_BL"))
                            Catch ex As Exception

                            End Try
                        Next
                    End If
                    If (da.Item("Type_Event") & "" = "C") Then
                        Dim dsBooking As DataTable = clsGet.Get_BookingOneRecord(da.Item("Booking_no")).Tables(0)
                        If (dsBooking.Rows.Count > 0) Then
                            If (remark.Trim() = "") Then
                                remark = dsBooking.Rows(0).Item("Vehicle_Text") & ""
                            Else
                                remark = "/" & dsBooking.Rows(0).Item("Vehicle_Text") & ""
                            End If
                        End If
                    Else
                        If (da.Item("Remark") & "" <> "") Then
                            If (remark = "") Then
                                remark = da.Item("Remark") & ""
                            Else
                                remark = "/" & da.Item("Remark") & ""
                            End If
                        End If
                    End If
                Next
            End If
            dr.Item("Tran1") = Cut
            dr.Item("Tran2") = Excise
            dr.Item("Tran3") = Cut + Excise
            dr.Item("R_Tran") = Recieve
            dr.Item("T_Tran") = Transfer
            dr.Item("Total") = (((Open - (Cut + Excise)) + Recieve) - Transfer)
            dr.Item("Remark") = remark
            dr.Item("External") = BL
            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Next
        ViewState("Marker") = dt
        gridMarker.DataSource = dt
        gridMarker.DataBind()
    End Sub

    Private Function InsertValue() As Boolean
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        tran = clsDB.BeginTran(conn)
        For i As Integer = 0 To gridMarker.Rows.Count - 1
            Dim txtRecieve As TextBox = CType(gridMarker.Rows(i).FindControl("txtRecieve"), TextBox)
            Dim txtLoss As TextBox = CType(gridMarker.Rows(i).FindControl("txtLoss"), TextBox)
            Dim txtPlus As TextBox = CType(gridMarker.Rows(i).FindControl("txtPlus"), TextBox)
            Dim txt282 As TextBox = CType(gridMarker.Rows(i).FindControl("txt282"), TextBox)
            Dim txt28 As TextBox = CType(gridMarker.Rows(i).FindControl("txt28"), TextBox)
            Dim txt36 As TextBox = CType(gridMarker.Rows(i).FindControl("txt36"), TextBox)
            Dim txtShore As TextBox = CType(gridMarker.Rows(i).FindControl("txtShore"), TextBox)

            Dim User_Create As String = CType(Session("UserData"), DataSet).Tables(0).Rows(0).Item("User_Name") & ""
            Dim Date_Time As String = Date.Now.ToString("dd MMM yy HH:mm:ss")
            clsFc.Delete_Marker_Report(gridMarker.DataKeys(i).Values("Stock_Code"), gridMarker.DataKeys(i).Values("_D"), conn, tran)
            If (clsFc.Insert_Marker_Report(gridMarker.DataKeys(i).Values("_D"), gridMarker.DataKeys(i).Values("Stock_Code"), txtRecieve.Text, txtLoss.Text, txtPlus.Text, txt282.Text, txt28.Text, txt36.Text, txtShore.Text, User_Create, Date_Time, conn, tran) < 1) Then
                condition = False
            End If
        Next

        If (condition) Then
            clsDB.CommitTran(tran)
        Else
            clsDB.RollBackTran(tran)
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('เกิดความผิดพลาดในการแสดงข้อมูล!!');", True)
        End If

        conn.Close()
        Application.UnLock()
        conn.Dispose()
        Return condition
    End Function
End Class